/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.dao;

import core.APersistenceManager;
import core.ITournamentDAO;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import persistence.*;
import validators.TournamentValidator;

/**
 *
 * @author Ivan
 */
public class TournamentDAO extends APersistenceManager<Tournament> implements ITournamentDAO {

    public TournamentDAO() {
        super(Tournament.class, new TournamentValidator());
    }

    @Override
    public Set<Tournament> getTournamentsByDiscipline(int idDiscipline) {
        Set<Tournament> tournaments = new HashSet<Tournament>();

        Discipline discipline = getEntityManager().find(Discipline.class, BigDecimal.valueOf(idDiscipline));
        for (League league : discipline.getLeagueSet()) {
            for (Tournament tournament : league.getTournamentSet()) {
                tournaments.add(tournament);
            }
        }
        return tournaments;
    }

    @Override
    public Tournament getTournamentByGame(int idGame) {
        Game game = getEntityManager().find(Game.class, BigDecimal.valueOf(idGame));
        return game.getTournamentId();
    }

    @Override
    public Set<Tournament> getTournamentsByLeague(int idLeague) {
        League league = getEntityManager().find(League.class, BigDecimal.valueOf(idLeague));
        return league.getTournamentSet();
    }

    @Override
    public Set<Tournament> getTournamentsByOwner(int idUser) {
        SysUser sysUser = getEntityManager().find(SysUser.class, BigDecimal.valueOf(idUser));
        return sysUser.getTournamentSet();
    }

    @Override
    public Team getWinner(int id) {
        Tournament tournament = getEntityManager().find(Tournament.class, BigDecimal.valueOf(id));
        return tournament.getWinnerTeamId();
    }
}
